//
//  CommentsCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/27.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "CommentsCell.h"
#import "ZXChatHelper.h"
#import "ImageCell.h"
#import "MSSBrowseModel.h"
#import "MSSBrowseNetworkViewController.h"
#import "OtherInfoVC.h"
@interface CommentsCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;

@end

@implementation CommentsCell{
    UIImageView *headImageV;
    QXLable     *labName;
    QXLable     *labTime;
    QXLable     *labContent;
    UIButton    *btnPraise;
    UIButton    *btnComments;
    
    UIButton    *lookMore;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        headImageV =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar"]];
        headImageV.layer.masksToBounds=YES;
        headImageV.layer.cornerRadius=19;
        headImageV.userInteractionEnabled=YES;
        UITapGestureRecognizer  *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickUserHead)];
        [headImageV addGestureRecognizer:tap];
        
        [self.contentView addSubview:headImageV];
        [headImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).with.offset(13);
            make.left.equalTo(self.contentView).with.offset(7);
            make.size.mas_equalTo(CGSizeMake(38, 38));
        }];
        
        labName=[[QXLable alloc]initTitle];
        labName.text=@"一只小蜜蜂";
        [self.contentView addSubview:labName];
        [labName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headImageV.mas_right).with.offset(10);
            make.top.equalTo(headImageV);
        }];
        
        labTime= [[QXLable alloc]initWithFount:13];
        labTime.text=@"2017-09-04 12:00";
        [self.contentView addSubview:labTime];
        [labTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labName);
            make.bottom.equalTo(headImageV);
        }];
        
        labContent =[[QXLable alloc]initTitle];
        [labContent use];
        labContent.text=@"如果你无法简洁的发表你的想法，那只说明你还不够了解它。";
        WS(weakSelf);
        labContent.selectBlock = ^(NSInteger i, NSAttributedString *attStr) {
            if (weakSelf.model.ParentComment.NickName.length>0) {
                if ([[NSString stringWithFormat:@"@%@",weakSelf.model.ParentComment.NickName] containsString:attStr.string]&&(i<weakSelf.model.ParentComment.NickName.length+weakSelf.model.Content.length+3)) {
                    if (weakSelf.lookUser) {
                        weakSelf.lookUser(weakSelf.model);
                    }
                }
            }
            
        };
        [self.contentView addSubview:labContent];
        [labContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTime);
            make.right.equalTo(self.contentView).with.offset(-20);
            make.top.equalTo(labTime.mas_bottom).with.offset(11);
        }];
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(labContent);
            make.top.equalTo(labContent.mas_bottom).with.offset(5);
            make.height.mas_equalTo(@0);
        }];
        
        UIImage *imageComments=[UIImage imageNamed:@"comments"];
        btnComments=[[UIButton alloc]init];
        [btnComments addTarget:self action:@selector(onclickReplay) forControlEvents:UIControlEventTouchUpInside];
        [btnComments setImage:imageComments forState:UIControlStateNormal];
        [self.contentView addSubview:btnComments];
        [btnComments mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(labContent);
            make.top.equalTo(self.collectionView.mas_bottom).with.offset(11);
            make.size.mas_equalTo(imageComments.size);
            make.bottom.equalTo(self.contentView).with.offset(-52);
        }];
        
        btnPraise=[[UIButton alloc]init];
        [btnPraise setImage:[UIImage imageNamed:@"praise_normal"] forState:UIControlStateNormal];
        [btnPraise setImage:[UIImage imageNamed:@"praise_select"] forState:UIControlStateSelected];
        [btnPraise setTitle:@"100" forState:UIControlStateNormal];
        [btnPraise setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
        [btnPraise addTarget:self action:@selector(onclickLike) forControlEvents:UIControlEventTouchUpInside];
        btnPraise.contentHorizontalAlignment=
        UIControlContentHorizontalAlignmentRight;
        [btnPraise.titleLabel setFont:[UIFont systemFontOfSize:10]];
        [self.contentView addSubview:btnPraise];
        [btnPraise mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(btnComments.mas_left).with.offset(-10);
            make.centerY.equalTo(btnComments);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
        
        
        lookMore =[[UIButton alloc]init];
        lookMore.backgroundColor=DEFAULTBGCOLOR;
        [lookMore setTitle:@"查看全部10条回复>>" forState:UIControlStateNormal];
        [lookMore addTarget:self action:@selector(onclickLook) forControlEvents:UIControlEventTouchUpInside];
        [lookMore setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
        [lookMore.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        [self.contentView addSubview:lookMore];
        [lookMore mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labContent);
            make.top.equalTo(btnComments.mas_bottom).with.offset(10);
            make.right.equalTo(self.contentView).with.offset(-10);
            make.bottom.equalTo(self.contentView).with.offset(-10);
           
        }];
        
        UIView  *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.equalTo(self.contentView);
            make.left.equalTo(headImageV);
            make.height.mas_equalTo(@.7);
        }];
    }
    return self;
}
///回复
- (void)onclickReplay{
    if (self.replayComment) {
        self.replayComment(self.model);
    }
}

///点赞
- (void)onclickLike{
    if (self.likeComment) {
        self.likeComment(self.model);
    }
}

///查看评论
- (void)onclickLook{
    if (self.lookComment) {
        self.lookComment(self.model);
    }
}

#pragma mark -- 前往用户信息
- (void)onclickUserHead{
//    OtherInfoVC *vc=[[OtherInfoVC alloc]init];
//    vc.userId=self.model.UserId;
//    [[TGTools viewController:self].navigationController pushViewController:vc animated:YES];
}

- (void)setModel:(CommentModel *)model{
    _model=model;
    [btnPraise setSelected:model.IsLike];
    [headImageV sd_setImageWithURL:[NSURL URLWithString:model.Avater]];
    labName.text=model.NickName;
    labTime.text=model.SubTime;
    
    NSString *content;
    if (model.ParentComment.Content.length>0) {
        content=[NSString stringWithFormat:@"%@//@%@:%@",model.Content,model.ParentComment.NickName,model.ParentComment.Content];
        NSAttributedString *str=[ZXChatHelper formatMessageString:content];
        
        NSRange rang=[str.string rangeOfString:[NSString stringWithFormat:@"@%@",model.ParentComment.NickName]];
        NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithAttributedString:str];
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#166cec"] range:rang];
        labContent.attributedText=attStr;
    }else{
        content=model.Content;
        labContent.attributedText=[ZXChatHelper formatMessageString:content];
    }
    
    [btnPraise setTitle:[NSString stringWithFormat:@" %ld",model.StarCount] forState:UIControlStateNormal];
    if (model.ReplayCount>0) {
        lookMore.hidden=NO;
        [lookMore setTitle:[NSString stringWithFormat:@"查看全部%ld条回复>>",model.ReplayCount] forState:UIControlStateNormal];
        [btnComments mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).with.offset(-52);
        }];
        [lookMore mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labContent);
            make.top.equalTo(btnComments.mas_bottom).with.offset(10);
            make.right.equalTo(self.contentView).with.offset(-10);
            make.bottom.equalTo(self.contentView).with.offset(-10);
        }];
    }else{
        lookMore.hidden=YES;
        [lookMore mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(labContent);
            make.size.mas_equalTo(CGSizeMake(0, 0));
        }];
        [btnComments mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).with.offset(-10);
        }];
    }
    if (model.Pictures.count>0) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@90);
        }];
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0);
        }];
    }
    
    
}


#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.Pictures.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH-80)/3.0, 80);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    [cell.imageV sd_setImageWithURL:self.model.Pictures[indexPath.row]];
    return cell;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 0, 5, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *imageArr=[[NSMutableArray alloc]init];
    for (int i=0; i<self.model.Pictures.count; i++) {
        ImageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        
        MSSBrowseModel *browseItem = [[MSSBrowseModel alloc]init];
        browseItem.smallImageView = cell.imageV;// 小图
        NSString *strImageUrl=self.model.Pictures[i];
        if ([strImageUrl containsString:@"_Min"]) {
            strImageUrl = [strImageUrl stringByReplacingOccurrencesOfString:@"_Min" withString:@""];
        }
        browseItem.bigImageUrl =[NSString stringWithFormat:@"%@", strImageUrl];
        [imageArr addObject:browseItem];
    }
    // 加载网络图片大图地址
    MSSBrowseNetworkViewController *bvc = [[MSSBrowseNetworkViewController alloc]initWithBrowseItemArray:imageArr currentIndex:indexPath.row];
    [bvc showBrowseViewController];
    
}


#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-85)/3.0, 80)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 10.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
    }
    return _collectionView;
}

@end
